1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.base;
18  
19  import static java.util.concurrent.TimeUnit.MICROSECONDS;
20  import static java.util.concurrent.TimeUnit.MILLISECONDS;
21  import static java.util.concurrent.TimeUnit.NANOSECONDS;
22  
23  import com.google.common.annotations.GwtCompatible;
24  import com.google.common.testing.FakeTicker;
25  
26  import junit.framework.TestCase;
27  
28  /**
29   * Unit test for {@link Stopwatch}.
30   *
31   * @author Kevin Bourrillion
32   */
33  @GwtCompatible(emulated = true)
34  public class StopwatchTest extends TestCase {
35  
36    private final FakeTicker ticker = new FakeTicker();
37    private final Stopwatch stopwatch = new Stopwatch(ticker);
38  
39    public void testCreateStarted() {
40      Stopwatch startedStopwatch = Stopwatch.createStarted();
41      assertTrue(startedStopwatch.isRunning());
42    }
43  
44    public void testCreateUnstarted() {
45      Stopwatch unstartedStopwatch = Stopwatch.createUnstarted();
46      assertFalse(unstartedStopwatch.isRunning());
47      assertEquals(0, unstartedStopwatch.elapsed(NANOSECONDS));
48    }
49  
50    public void testInitialState() {
51      assertFalse(stopwatch.isRunning());
52      assertEquals(0, stopwatch.elapsed(NANOSECONDS));
53    }
54  
55    public void testStart() {
56      assertSame(stopwatch, stopwatch.start());
57      assertTrue(stopwatch.isRunning());
58    }
59  
60    public void testStart_whileRunning() {
61      stopwatch.start();
62      try {
63        stopwatch.start();
64        fail();
65      } catch (IllegalStateException expected) {
66      }
67      assertTrue(stopwatch.isRunning());
68    }
69  
70    public void testStop() {
71      stopwatch.start();
72      assertSame(stopwatch, stopwatch.stop());
73      assertFalse(stopwatch.isRunning());
74    }
75  
76    public void testStop_new() {
77      try {
78        stopwatch.stop();
79        fail();
80      } catch (IllegalStateException expected) {
81      }
82      assertFalse(stopwatch.isRunning());
83    }
84  
85    public void testStop_alreadyStopped() {
86      stopwatch.start();
87      stopwatch.stop();
88      try {
89        stopwatch.stop();
90        fail();
91      } catch (IllegalStateException expected) {
92      }
93      assertFalse(stopwatch.isRunning());
94    }
95  
96    public void testReset_new() {
97      ticker.advance(1);
98      stopwatch.reset();
99      assertFalse(stopwatch.isRunning());
100     ticker.advance(2);
101     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
102     stopwatch.start();
103     ticker.advance(3);
104     assertEquals(3, stopwatch.elapsed(NANOSECONDS));
105   }
106 
107   public void testReset_whileRunning() {
108     ticker.advance(1);
109     stopwatch.start();
110     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
111     ticker.advance(2);
112     assertEquals(2, stopwatch.elapsed(NANOSECONDS));
113     stopwatch.reset();
114     assertFalse(stopwatch.isRunning());
115     ticker.advance(3);
116     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
117   }
118 
119   public void testElapsed_whileRunning() {
120     ticker.advance(78);
121     stopwatch.start();
122     assertEquals(0, stopwatch.elapsed(NANOSECONDS));
123 
124     ticker.advance(345);
125     assertEquals(345, stopwatch.elapsed(NANOSECONDS));
126   }
127 
128   public void testElapsed_notRunning() {
129     ticker.advance(1);
130     stopwatch.start();
131     ticker.advance(4);
132     stopwatch.stop();
133     ticker.advance(9);
134     assertEquals(4, stopwatch.elapsed(NANOSECONDS));
135   }
136 
137   public void testElapsed_multipleSegments() {
138     stopwatch.start();
139     ticker.advance(9);
140     stopwatch.stop();
141 
142     ticker.advance(16);
143 
144     stopwatch.start();
145     assertEquals(9, stopwatch.elapsed(NANOSECONDS));
146     ticker.advance(25);
147     assertEquals(34, stopwatch.elapsed(NANOSECONDS));
148 
149     stopwatch.stop();
150     ticker.advance(36);
151     assertEquals(34, stopwatch.elapsed(NANOSECONDS));
152   }
153 
154   public void testElapsed_micros() {
155     stopwatch.start();
156     ticker.advance(999);
157     assertEquals(0, stopwatch.elapsed(MICROSECONDS));
158     ticker.advance(1);
159     assertEquals(1, stopwatch.elapsed(MICROSECONDS));
160   }
161 
162   public void testElapsed_millis() {
163     stopwatch.start();
164     ticker.advance(999999);
165     assertEquals(0, stopwatch.elapsed(MILLISECONDS));
166     ticker.advance(1);
167     assertEquals(1, stopwatch.elapsed(MILLISECONDS));
168   }
169 
170   public void testElapsedMillis() {
171     stopwatch.start();
172     ticker.advance(999999);
173     assertEquals(0, stopwatch.elapsed(MILLISECONDS));
174     ticker.advance(1);
175     assertEquals(1, stopwatch.elapsed(MILLISECONDS));
176   }
177 
178   public void testElapsedMillis_whileRunning() {
179     ticker.advance(78000000);
180     stopwatch.start();
181     assertEquals(0, stopwatch.elapsed(MILLISECONDS));
182 
183     ticker.advance(345000000);
184     assertEquals(345, stopwatch.elapsed(MILLISECONDS));
185   }
186 
187   public void testElapsedMillis_notRunning() {
188     ticker.advance(1000000);
189     stopwatch.start();
190     ticker.advance(4000000);
191     stopwatch.stop();
192     ticker.advance(9000000);
193     assertEquals(4, stopwatch.elapsed(MILLISECONDS));
194   }
195 
196   public void testElapsedMillis_multipleSegments() {
197     stopwatch.start();
198     ticker.advance(9000000);
199     stopwatch.stop();
200 
201     ticker.advance(16000000);
202 
203     stopwatch.start();
204     assertEquals(9, stopwatch.elapsed(MILLISECONDS));
205     ticker.advance(25000000);
206     assertEquals(34, stopwatch.elapsed(MILLISECONDS));
207 
208     stopwatch.stop();
209     ticker.advance(36000000);
210     assertEquals(34, stopwatch.elapsed(MILLISECONDS));
211   }
212 
213 }
214